package com.zekai.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.ExamDetail;
import com.zekai.dao.IExamDetailDao;

import sun.net.www.content.audio.basic;

public class ExamDetailDaoImpl extends BaseDao implements IExamDetailDao{

	@Override
	public String getNameById(int id) {
		String sql="select sub_detail from exam_detail where id=?";
		String name=null;
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection=getConnection();
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setObject(1, id);
			resultSet=preparedStatement.executeQuery();
			if (resultSet.next()) {
				name=resultSet.getString("sub_detail");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return name;
	}

	@Override
	public Object getEntity(ResultSet rs) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDruation(String name) {
		String sql="select duration from exam_detail where sub_detail=?";
		List<ExamDetail> list=extuteQuery(sql, name);
		return list.get(0).getDuration();
	}

	@Override
	public List<String> getSubDetailName(int subNumber) {
		List<String> subDetail=new ArrayList<String>();
		
		String sql="SELECT sub_detail FROM exam_detail WHERE subNumber=?";
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection=getConnection();
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setObject(1, subNumber);
			resultSet=preparedStatement.executeQuery();
			
			while (resultSet.next()) {
				subDetail.add(resultSet.getString("sub_detail"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return subDetail;
	}

	@Override
	public int getSubDetailIdByName(String name) {
		int id=0;
		String sql="SELECT id FROM exam_detail WHERE sub_detail=?";
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection=getConnection();
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setObject(1, name);
			resultSet=preparedStatement.executeQuery();
			if (resultSet.next()) {
				id=resultSet.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return id;
	}

}
